package handlers

import (
	"backEnd/database"
	"backEnd/models"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

// GetHomeBanner 获取首页海报列表
func GetHomeBanner(c *gin.Context) {
	startTime := time.Now()

	query := `
		SELECT id, sort, target, picurl, url, appid 
		FROM home_banner 
		ORDER BY sort ASC
	`

	rows, err := database.DB.Query(query)
	if err != nil {
		c.JSON(http.StatusOK, models.Response{
			ErrCode:  400,
			ErrMsg:   "查询失败",
			Data:     nil,
			TimeCost: time.Since(startTime).Milliseconds(),
		})
		return
	}
	defer rows.Close()

	var banners []models.HomeBanner
	for rows.Next() {
		var banner models.HomeBanner
		err := rows.Scan(&banner.ID, &banner.Sort, &banner.Target, &banner.PicURL, &banner.URL, &banner.AppID)
		if err != nil {
			continue
		}
		banners = append(banners, banner)
	}

	c.JSON(http.StatusOK, models.Response{
		ErrCode:  0,
		ErrMsg:   "查询成功",
		Data:     banners,
		TimeCost: time.Since(startTime).Milliseconds(),
	})
}
